package vincent.com.layouttest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends FragmentActivity {
    private final String TAG = "MainActivity";
    private ImageView launcher;
    private final int MSG_SHOW_ANIM = 1;

    private float srcYPosition;
    private float yDistance = 0;
    private boolean isTouching = false;
    private int launcherHeight = 0;
    private View mView;

    private RotateAnimation rotateIconAnim;
    private LinearLayout launcherContainer;
    private boolean isRefreshing = false;

    private Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_SHOW_ANIM:
                    rotateIconAnim = new RotateAnimation(0f, 360f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                    rotateIconAnim.setDuration(500);
                    rotateIconAnim.setInterpolator(MainActivity.this, android.R.interpolator.accelerate_decelerate);
                    rotateIconAnim.setRepeatCount(3);

                    rotateIconAnim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            isRefreshing = true;
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            isRefreshing = false;
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });

                    launcher.startAnimation(rotateIconAnim);

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = getLayoutInflater().inflate(R.layout.activity_main, null);
        setContentView(mView);

        launcher = (ImageView)findViewById(R.id.launcher);
        launcherContainer = (LinearLayout)findViewById(R.id.launcher_container);

       /* ViewTreeObserver layoutObserver = mView.getViewTreeObserver();
        layoutObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                launcherHeight = launcher.getMeasuredHeight();
                srcYPosition = -(srcYPosition + launcherHeight/2 + 20);
                launcher.setY(srcYPosition);

                mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });*/

        findViewById(R.id.layout_container).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){

                    case MotionEvent.ACTION_DOWN:
                        if(isRefreshing){
                            return true;
                        }

                        isTouching = true;
                        yDistance = event.getY() - srcYPosition;
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        if(isRefreshing){
                            return true;
                        }

                        float displaceMent = event.getY() - yDistance;
                        if(displaceMent < 500 && displaceMent > -20){
                            launcherContainer.setY(displaceMent + srcYPosition);
                        }
                        return true;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        if(isRefreshing){
                            return true;
                        }

                        isTouching = false;
                        yDistance = 0;
                        myHandler.sendEmptyMessage(MSG_SHOW_ANIM);
                        return true;
                }
                return false;
            }
        });
    }
}
